package com.zf.zffencheng.controller;

import com.zf.zffencheng.model.Result;
import com.zf.zffencheng.model.dto.AdminLoginDTO;
import com.zf.zffencheng.model.dto.ResetPwdDTO;
import com.zf.zffencheng.model.dto.UpdatePwdDTO;
import com.zf.zffencheng.model.pojo.TAdmin;
import com.zf.zffencheng.model.vo.AdminLoginInfoVo;
import com.zf.zffencheng.service.AdminService;
import com.zf.zffencheng.service.SmsService;
import com.zf.zffencheng.util.BeanUtils;
import com.zf.zffencheng.util.UserContextUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName: AdminLoginController <br/>
 * Description:  <br/>
 * date: 2021/5/8 16:28<br/>
 *
 * @author william <br/>
 */
@RestController
@RequestMapping("/sapi/v1/login/")
@Api(tags = "登录")
public class AdminLoginController {

    private final AdminService adminService;
    private final SmsService smsService;

    @Autowired
    public AdminLoginController(AdminService adminService, SmsService smsService) {
        this.adminService = adminService;
        this.smsService = smsService;
    }

    @PostMapping("admin")
    @ApiOperation("用户登录")
    @ApiResponses( value = @ApiResponse(code = 201, message = "", response =  AdminLoginInfoVo.class))
    public Result login(@RequestBody AdminLoginDTO adminLoginDTO){
        AdminLoginInfoVo adminLoginInfoVo = adminService.login(adminLoginDTO);
        return Result.ok(adminLoginInfoVo);
    }

    @PostMapping("forget/pwd")
    @ApiOperation("忘记密码")
    public Result resetPwd(@RequestBody ResetPwdDTO resetPwdDTO) throws Exception {
        adminService.resetPwd(resetPwdDTO);
        return Result.ok();
    }

    @PostMapping("update/pwd")
    @ApiOperation("修改密码")
    public Result updatePwd(@RequestBody UpdatePwdDTO updatePwdDTO) throws Exception {
        AdminLoginInfoVo currentAdminInfo = UserContextUtils.getCurrentAdminInfo();
        Integer adminId = currentAdminInfo.getAdminId();
        TAdmin tAdmin = adminService.getById(adminId);
        ResetPwdDTO resetPwdDTO = BeanUtils.copyAs(updatePwdDTO, ResetPwdDTO.class);
        //账号
        resetPwdDTO.setMobile(tAdmin.getAdminNo());
        //修改密码
        adminService.resetPwd(resetPwdDTO);
        return Result.ok();
    }

    @PostMapping("reset/pay/pwd")
    @ApiOperation("重置支付密码")
    public Result resetPayPwd(@RequestBody UpdatePwdDTO updatePwdDTO){
        AdminLoginInfoVo currentAdminInfo = UserContextUtils.getCurrentAdminInfo();
        Integer adminId = currentAdminInfo.getAdminId();
        currentAdminInfo.setAdminId(adminId);
        adminService.resetPayPwd(updatePwdDTO);
        return Result.ok();
    }

    @ApiOperation("当前用户发送短信")
    @PostMapping("send/sms")
    public Result sendSms() throws Exception {
        AdminLoginInfoVo currentAdminInfo = UserContextUtils.getCurrentAdminInfo();
        Integer adminId = currentAdminInfo.getAdminId();
        TAdmin tAdmin = adminService.getById(adminId);
        //随机生成
        String code = RandomStringUtils.random(6, false, true);
        smsService.send(tAdmin.getAdminNo(), code);
        return Result.ok();
    }



}
